package com.mrobuy.jd.category;

import java.io.File;
import java.nio.charset.Charset;
import java.util.List;

import com.gavel.database.SQLExecutor;
import com.google.common.io.Files;
import com.google.gson.Gson;
import com.jd.open.api.sdk.domain.list.CategoryReadService.response.findByPId.Category;
import com.mrobuy.jdvc.entity.JDCategory;
import com.mrobuy.util.IdentifierGen;

/**
 * 京东类目加载
 */
public class JDCategoryLoader {

    public static void main(String[] args) throws Exception {

      List<String> lines =  Files.readLines(new File("category_1.txt"), Charset.forName("UTF8"));


        for (String line : lines) {
            Category category = new Gson().fromJson(line, Category.class);

            JDCategory jdCategory1 = SQLExecutor.executeQueryBean("select * from JD_CATEGORY where CODE = 'jsyg' and CID = ? ", JDCategory.class, category.getId());

            boolean insert = (jdCategory1 ==null);
            if ( insert ) {
                jdCategory1 = new JDCategory();
                jdCategory1.setId(IdentifierGen.uuid());
            }

            jdCategory1.setCode("jsyg");
            jdCategory1.setCid(category.getId().intValue());
            jdCategory1.setPid(category.getFid().intValue());
            jdCategory1.setName(category.getName());
            jdCategory1.setLevel(category.getLev());
            jdCategory1.setSeq(category.getOrder());
            jdCategory1.setFeature(new Gson().toJson(category.getFeatures()));

            if ( insert ) {
                SQLExecutor.insert(jdCategory1);
            } else {
                SQLExecutor.update(jdCategory1);
            }

            System.out.println(category.getName());

        }

    }
}
